﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FonShare.Classes.FTP
{
    /// <summary>
    /// Describe information about a FTP file.
    /// </summary>
    /// <author>spesme</author>
    /// <since>24/06/2009</since>
    public class FS_FTPFile
    {
        public const int GIGA = 1000000000;
        public const int MEGA = 1000000;
        public const int KILO = 1000;

        /// <summary>
        /// File extensions.
        /// </summary>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static class Extension
        {
            public static List<string> Film = new List<string>() { "avi", "mkv", "mov", "wmv"};
            public static List<string> Music = new List<string>() { "mp3", "wav", "wma" };
            public static List<string> Book = new List<string>() { "pdf", "chm" };
            public static List<string> Archive = new List<string>() { "zip", "rar", "7z" };
            public static List<string> Image = new List<string>() { "png", "jpg", "gif", "bmp", "jpeg", "tiff" };
        }

        /// <summary>
        /// File Types.
        /// </summary>
        public enum FileType
        {
            Archive,
            Film,
            Music,
            Book,
            DVD,
            Image,
            Unknow
        }

        public string Directory;
        public string Name;
        public string Date;
        public long Size;
        public FileType Type;
        public bool IsDirectory;
        

        /// <summary>
        /// Initializes a new instance of the <see cref="FS_FTPFile"/> class.
        /// </summary>
        /// <param name="p_ftpresult">The result string from FTP Server.</param>
        /// <author>spesme</author>
        /// <since>24/06/2009</since>
        public FS_FTPFile(string p_ftpresult, string p_folder)
        {
            // Define is the current file is a directory.
            IsDirectory = p_ftpresult.StartsWith("d");
            // Split string as table to extract data.
            string[] ftpparts = p_ftpresult.Split(new char[] {' '});
            int i = 4;
            // Define size for file.
            for (int j = i; j < ftpparts.Length - 1; j++)
            {
                if (!string.IsNullOrEmpty(ftpparts[j]))
                {
                    Size = long.Parse(ftpparts[j]);
                    i = j;
                    break;
                }
            }
            // Define Date for File.
            Date = ftpparts[i+1] + " " + ftpparts[i+2];
            Name = string.Empty;
            bool isFilenameOver = false;
            // Read table from the end to reconstruct filename.
            for (int k=ftpparts.Length-1; !isFilenameOver;k--) 
            {
                int tmpResult = 0;
                if (ftpparts[k].Contains(":") || (ftpparts[k].Length == 4 && int.TryParse(ftpparts[k], out tmpResult)))
                {
                    // Stop when table value contains "xx:xx" or year.
                    isFilenameOver = true;
                }
                else
                {
                    Name = ftpparts[k] + " " + Name;
                }
            }
            Name = Name.Replace('\r', ' ').Trim();
            Directory = p_folder;
            Type = DefineType(Name);
        }

        /// <summary>
        /// Displays the size with suffix as GB, MB, KB or B.
        /// </summary>
        /// <returns></returns>
        /// <author>spesme</author>
        /// <since>24/06/2009</since>
        public string DisplaySize()
        {
            string result = null;
            if (Size > GIGA)
            {
                result = Size / GIGA + " GB";
            }
            else if (Size > MEGA)
            {
                result = Size / MEGA + " MB";
            }
            else if (Size > KILO)
            {
                result = Size / KILO + " KB";
            }
            else
            {
                result = Size.ToString() + " B";
            }
            return result;
        }

        /// <summary>
        /// Defines the type of the file by examining its extension.
        /// </summary>
        /// <param name="p_filename">The filename.</param>
        /// <returns>The FileType of the file.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static FileType DefineType(string p_filename)
        {
            if (Extension.Film.Where(record => p_filename.EndsWith(record)).Count() == 1)
            {
                return FileType.Film;
            }
            else if (Extension.Music.Where(record => p_filename.EndsWith(record)).Count() == 1)
            {
                return FileType.Music;
            }
            else if (Extension.Book.Where(record => p_filename.EndsWith(record)).Count() == 1)
            {
                return FileType.Book;
            }
            else if (Extension.Archive.Where(record => p_filename.EndsWith(record)).Count() == 1)
            {
                return FileType.Archive;
            }
            else if (Extension.Image.Where(record => p_filename.EndsWith(record)).Count() == 1)
            {
                return FileType.Image;
            }
            else
            {
                return FileType.Unknow;
            }
        }
    }
}
